package com.javatip.project.config;

import com.javatip.project.interceptor.RegistrationCodeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.function.Predicate;

@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private RegistrationCodeInterceptor registrationCodeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(registrationCodeInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/css/**", "/js/**", "/images/**", "/static/**", "/webjars/**",
                                      "/swagger-ui.html","/doc.html","/swagger-resources/**", "/v2/**"); // 排除静态资源
    }

    /**
     //     * 通过knife4j生成接口文档
     //     * @return
     //     */
    @Bean
    public Docket docket() {
        log.info("开始生成接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("智眼图像识别项目接口文档")
                .version("2.0")
                .description("智眼图像识别项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(Predicate.not(RequestHandlerSelectors.basePackage("com.javatip.project.deep_learning.app01Slidemodel"))) // 排除com.xxx包路径
                .apis(Predicate.not(RequestHandlerSelectors.basePackage("com.javatip.project.deep_learning.app01Outcome"))) // 排除com.xxx包路径
                .apis(RequestHandlerSelectors.basePackage("com.javatip.project.deep_learning") // 选择第一个包路径
                        .or(RequestHandlerSelectors.basePackage("com.javatip.project.Controller"))
                        .or(RequestHandlerSelectors.basePackage("com.javatip.project.detection"))) // 添加新的包路径.paths(PathSelectors.any())
                .build();
        return docket;
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取当前项目根目录
        String projectDir = System.getProperty("user.dir");

        // 定义相对路径
        String trainResultPath = projectDir + "/src/main/resources/trainResult/";
        String datasetsPath = projectDir + "/src/main/resources/datasets/";
        String changeDatasetsPath = projectDir + "/src/main/resources/changeDatasets/";
        String predictResultPath = projectDir + "/src/main/resources/predictResult/";
        String changePredictResultPath = projectDir + "/src/main/resources/changepredictResult/";
        String detectionDatasetPath = projectDir + "/src/main/resources/detectionDataset/";

        // 映射trainResult路径
        registry.addResourceHandler("/trainResult/**")
                .addResourceLocations("file:" + trainResultPath);


        // 映射datasets路径
        registry.addResourceHandler("/datasets/**")
                .addResourceLocations("file:" + datasetsPath);

        // 映射datasets路径
        registry.addResourceHandler("/changeDatasets/**")
                .addResourceLocations("file:" + changeDatasetsPath);

        // 映射predictResult路径
        registry.addResourceHandler("/predictResult/**")
                .addResourceLocations("file:" + predictResultPath);

        // 映射changepredictResult路径
        registry.addResourceHandler("/changepredictResult/**")
                .addResourceLocations("file:" + changePredictResultPath);

        // 映射detectionData路径
        registry.addResourceHandler("/detectionDataset/**")
                .addResourceLocations("file:" + detectionDatasetPath);

        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");

    }

}
